﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.IO;
using System.Threading.Tasks;
using System.Xml;
using System.Xml.Linq;


namespace DefineGenerated.DAL
{
    public struct FieldFuncInfo
    {
        public string Field;
        public string Func;
        public string FieldString;
        public string FuncString;
        public string FuncBind;

        public string FieldFind;


    }
    public class DefineHelp
    {

        string m_xmlPath;
        string m_xmlName;
        public string SavePath;

        XmlDocument m_xmlDocument;
        List<XmlLinkedNode> m_xmlLinkedNodes = new List<XmlLinkedNode>();
        public DefineHelp(string _xmlPath, string _SavePath)
        {
            Load(_xmlPath, _SavePath);
        }
        public DefineHelp() { }
        public void Load(string _xmlPath, string _SavePath)
        {
            m_xmlPath = _xmlPath;
            m_xmlName = Path.GetFileNameWithoutExtension(m_xmlPath);
            m_xmlDocument = new XmlDocument();
            m_xmlDocument.Load(m_xmlPath);
            SavePath = _SavePath;
        }


        //获取一个元素信息
        public FieldFuncInfo GetOne(XmlElement _xmlElement)
        {
            FieldFuncInfo ot_fieldFuncInfo = new FieldFuncInfo();
            ot_fieldFuncInfo.Field = _xmlElement.GetAttribute("name");
            if (ot_fieldFuncInfo.Field != "")
            {
                ot_fieldFuncInfo.FieldString =
                    string.Format("C{0}UI* {1};", _xmlElement.Name, ot_fieldFuncInfo.Field);

                ot_fieldFuncInfo.FieldFind = string.Format(
                    "{0} = static_cast<C{1}UI*>(m_pm.FindControl(_T(\"{2}\")));",
                    ot_fieldFuncInfo.Field, _xmlElement.Name, ot_fieldFuncInfo.Field
                    );

                ot_fieldFuncInfo.Func = _xmlElement.GetAttribute("BindFunc");
                if (ot_fieldFuncInfo.Func != "")
                {
                    ot_fieldFuncInfo.FuncString = string.Format(
                        "virtual bool {0}(void* _msg) {{return false;}}",
                        ot_fieldFuncInfo.Func
                        );

                    string xmlName = Path.GetFileNameWithoutExtension(_xmlElement.BaseURI);

                    ot_fieldFuncInfo.FuncBind = string.Format(
                         "{0}->OnNotify += MakeDelegate(this, &{1}PreDefine::{2});",
                        ot_fieldFuncInfo.Field, xmlName, ot_fieldFuncInfo.Func
                        );
                }
            }

            return ot_fieldFuncInfo;
        }
        //获取所有元素信息
        public FieldFuncInfo[] GetAll()
        {
            List<XmlLinkedNode> xls = new List<XmlLinkedNode>();
            XmlHelp.EnumeratorXML(m_xmlDocument.GetEnumerator(), xls, (x) =>
            {
                do
                {
                    if (x.NodeType != XmlNodeType.Element) break;
                    if (x.Name == "Font") break;
                    if (x.Name == "Default") break;
                    if (x.Name == "RefClass") break;
                    if (x.Name == "Window") break;

                    return true;
                } while (true);
                return false;
            });
            List<FieldFuncInfo> ot_fieldFuncInfos = new List<FieldFuncInfo>();

            for (int i = 0; i < xls.Count; i++)
            {
                FieldFuncInfo temp = GetOne((XmlElement)xls[i]);
                if (temp.Field != "")
                {
                    ot_fieldFuncInfos.Add(temp);
                }

            }
            return ot_fieldFuncInfos.ToArray();
        }
        //生成头文件
        public void Generated()
        {
            string hppText = Resource1.hppTemplate;
            hppText = hppText.Replace("{0}", m_xmlName);
            hppText = hppText.Replace("{1}", m_xmlName);
            hppText = hppText.Replace("{2}", m_xmlName);

            //生成字段
            StringBuilder FieldBuilder = new StringBuilder();
            FieldFuncInfo[] ffi = GetAll();
            foreach (var item in ffi)
            {
                FieldBuilder.AppendLine(item.FieldString);
            }
            hppText = hppText.Replace("{3}", FieldBuilder.ToString());
            //生成查找
            FieldBuilder.Clear();
            foreach (var item in ffi)
            {
                FieldBuilder.AppendLine(item.FieldFind);
            }
            hppText = hppText.Replace("{4}", FieldBuilder.ToString());
            //生成函数
            FieldBuilder.Clear();
            foreach (var item in ffi)
            {
                FieldBuilder.AppendLine(item.FuncString);
            }
            hppText = hppText.Replace("{5}", FieldBuilder.ToString());
            //生成函数绑定
            FieldBuilder.Clear();
            foreach (var item in ffi)
            {
                FieldBuilder.AppendLine(item.FuncBind);
            }
            hppText = hppText.Replace("{6}", FieldBuilder.ToString());

            string hppName = m_xmlName + "PreDefine.hpp";
            File.WriteAllText(SavePath + "\\" + hppName, hppText);

        }


    }
}
